﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>地图bounds查询</title>

    <link href="http://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <link href="http://cdn.bootcss.com/openlayers/4.2.0/ol.css" rel="stylesheet">
</head>
<body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%">
<div id="map" style="width: 100%;height:100%"></div>

<script src="http://cdn.bootcss.com/openlayers/4.2.0/ol.js"></script>
<script type="text/javascript" src="../../dist/iclient9-openlayers.min.js"></script>
<script type="text/javascript">
    var map, polygon, vectorLayer, resultLayer,
        url = "http://support.supermap.com.cn:8090/iserver/services/map-world/rest/maps/World";
    map = new ol.Map({
        target: 'map',
        controls: ol.control.defaults({attributionOptions: {collapsed: false}})
            .extend([new ol.supermap.control.Logo()]),
        view: new ol.View({
            center: [0, 0],
            zoom: 2,
            projection: 'EPSG:4326'
        })
    });
    var layer = new ol.layer.Tile({
        source: new ol.source.TileSuperMapRest({
            url: url
        }),
        projection: 'EPSG:4326'
    });
    map.addLayer(layer);
    query();

    function query() {
        polygon = new ol.geom.Polygon([[[0, 0], [60, 0], [60, 39], [0, 39], [0, 0]]]);
        var polygonSource = new ol.source.Vector({
            features: [new ol.Feature(polygon)],
            wrapX: false
        });
        vectorLayer = new ol.layer.Vector({
            source: polygonSource,
            style: new ol.style.Style({
                stroke: new ol.style.Stroke({
                    color: 'blue',
                    width: 3
                }),
                fill: new ol.style.Fill({
                    color: 'rgba(0, 0, 255, 0.1)'
                })
            })
        });
        map.addLayer(vectorLayer);

        var param = new SuperMap.QueryByBoundsParameters({
            queryParams: {name: "Capitals@World.1"},
            bounds: polygon.getExtent()
        });
        new ol.supermap.QueryService(url).queryByBounds(param, function (serviceResult) {
            var vectorSource = new ol.source.Vector({
                features: (new ol.format.GeoJSON()).readFeatures(serviceResult.result.recordsets[0].features),
                wrapX: false
            });
            resultLayer = new ol.layer.Vector({
                source: vectorSource
            });
            map.addLayer(resultLayer);
        });
    }
</script>
</body>
</html>